//Edited by Jon & Tankbuster
//#define __DEBUG__
#define THIS_FILE "x_m103.sqf"
#include "x_setup.sqf"
private ["_timewarned", "_contact","_mpos","_mpos2","_mpos3","_waittime","_pos_other","_pos_other2","_civgrp125","_civ","_poss","_nobjs","_pos_other","_cman","_drugdealer","_drugdealers","_pos_other2","_drugs","_truck","_strider","_aliveddealers","_alivetruck","_alivestrider"];

_timewarned = false;
_contact = false;
 
_mpos = markerPos QGVAR(sm_603);// Meeting point with CIA Contact.
_mpos set [2,0];
_mpos2 = markerPos QGVAR(sm_603_1);// Viewpoint Location to be revealed When your shown how to get there... 
_mpos2 set [2,0];
_mpos3 = markerPos QGVAR(sm_603_2);// Actual Location Of Drug Smugglers & The truck etc.
_mpos3 set [2,0];
GVAR(x_sm_pos) = [_mpos, _mpos2, _mpos3]; // index: 103,   Destroy The Drug Smugglers & The truck etc.
GVAR(x_sm_type) = "normal"; // "convoy"

#ifndef __TT__
GVAR(current_sm_bonus_vec) = "B_Heli_Attack_01_F";//
#else
GVAR(current_sm_bonus_vec) = ["B_Heli_Attack_01_F"];
#endif
	
#ifdef __SMMISSIONS_MARKER__
if (true) exitWith {};
#endif
_waittime = (time + (25 * 60));// Setting Wait Time for initial Meet up with CIA Contact (15minutes)

if (X_Client && {!GVAR(IS_HC_CLIENT)}) then {
	GVAR(cur_sm_txt) = (localize "STR_DOM_MISSIONSTRING_5072");// "Get to the Map Location to meet the CIA Contact, he has information for your Team"
	GVAR(current_mission_resolved_text) = (localize "STR_DOM_MISSIONSTRING_5073");//"Good Job guys! That should stop them for a while."
};
// Create CIA Contact for the 'Meeting' at the First marker SM_126
if (call FUNC(checkSHC)) then {
    __PossAndOther
	
	_pos_other = GVAR(x_sm_pos) select 1;
    _pos_other2 = GVAR(x_sm_pos) select 2;

	_civgrp125 = createGroup WEST;
	_civ = _civgrp125 createUnit ["B_Competitor_F",_poss, [], 0, "NONE"];
	removeHeadgear _civ;
	removeUniform _civ;
	_civ setFace "AfricanHead_01";
	_civ addHeadgear "H_Cap_brn_SPECOPS";
	_civ addUniform "U_Rangemaster";
	_civ addBackPack "B_FieldPack_blk";
    _civ addweapon "SMG_01_Holo_F";
   	_civ allowFleeing 0;
	_civ disableAI "Move"; 
	_civ setBehaviour "Stealth";
	
	doStop _civ;
	_civ addEventHandler ["killed", {GVAR(sm_winner) = -1; GVAR(sm_resolved) = true;}];
	while {(!GVAR(sm_resolved)) && {count ((getpos _civ) nearEntities ["B_Soldier_base_F", 5]) < 2}} do {
		sleep 5.123;
		if (!_timewarned && {time > (_waittime - 180)}) then {
			[QGVAR(kbmsg), [48]] call FUNC(NetCallEventCTS);// message to Actual... "Hurry up, The Contact wont wait for ever"
			_timewarned = true;
		};
		if (time > _waittime) exitWith {
			GVAR(sm_winner) = -881;//You have run out of time...
			GVAR(sm_resolved) = true;	
		};
	};
		
	// You arrive at The Map Location and Meet the contact....
	if (time < _waittime && {!_contact}) then {
		_nobjs = _civ nearEntities ["CAManBase", 35];
		if (count _nobjs > 1) then {
			{
				if (isPlayer _x && {alive _x}) exitWith {
					_contact = true;
					_civ enableAI "MOVE";// This Allows him to move
					[QGVAR(setcapt), [_civ, true]] call FUNC(NetCallEvent);
					
					[QGVAR(kbmsg), [54]] call FUNC(NetCallEventCTS);//message to Actual from CIA Contact... "Follow me, I will show you the location of the Drug smugglers"
				};
			} forEach _nobjs;
		};
	};
		
	_civ enableAI "MOVE";// This Allows him to move
	_civ allowFleeing 0;
	_civ setSpeedMode "LIMITED";
	_civ setBehaviour "CARELESS";
	sleep 2;
	_civ doMove _pos_other;//and will lead all players in the group  to sm_103_1

	sleep 2.123;// this spawns the Pickups, MG Pickups, the Drug smugglers and their shipment
	if (GVAR(sm_resolved)) exitWith {};
	private "_drugdealers";
	if (isNil "_drugdealers") then {
		_drugdealers = [];
	};
	__GetEGrp(_drugdealersgr)
	for "_i" from 0 to 9 do {
		_cman = "I_G_Soldier_M_F";
		_drugdealer = _drugdealersgr createUnit [_cman, _pos_other2, [], 10, "FORM"];
		_drugdealer enableAI "MOVE";
		_drugdealers set [count _drugdealers, _drugdealer];
    };
	sleep 0.551;
		
	_drugdealersgr allowFleeing 0;
	_drugdealersgr setCombatMode "RED";
	_drugdealersgr setBehaviour "AWARE";
	
	for "_i" from 0 to 4 do {
		_drugs = createVehicle ["Land_CratesWooden_F", _pos_other2, [], 2, "NONE"];
		__AddToExtraVec(_drugs)
		sleep 0.987;
	};
	_pickup = createVehicle ["I_G_Offroad_01_F", _pos_other2, [], 2, "NONE"];
	__AddToExtraVec(_pickup)
	_pickupmg = createVehicle ["I_G_Offroad_01_armed_F", _pos_other2, [], 2, "NONE"];
	__AddToExtraVec(_pickupmg)
	sleep 1.987;	
	// Your team and the CIA contact Arrive at the Target Map Location
	
    while {alive _civ && {(_civ distance _pos_other)> 5}} do {
		sleep 5;
	};
 
   [QGVAR(kbmsg), [56]] call FUNC(NetCallEventCTS);//message to Actual from CIA Contact...55 = " We are here, Look There they are. Good Luck My friends, I must leave now" 56 = "Look There they are, destroy the vehicles, the crates and kill the soldiers. Good Luck, I must leave now"

	sleep 15.5;
   _civ doMove _poss;
   _nobjs = _civ nearEntities ["CAManBase", 35];
	if (count _nobjs < 1) then {
		deleteVehicle _civ; // remove cia
	};
	//completion lines ->
	while {!GVAR(sm_resolved)} do {
		_aliveddealers = {alive _x} count _drugdealers;
		_pickup = _pos_other2 nearobjects ["Offroad_01_base_F", 50];//Base class for pickup
		_alivepickup = {alive _x} count _pickup;
		_pickupmg = _pos_other2 nearobjects ["Offroad_01_base_F", 50];//Base class for pickup
		_alivepickupmg = {alive _x} count _pickupmg;
		sleep 0.987;// Checks to see how many DrugDealers,pickups and mgpickups are alive then add in win completion if all dead......
		if (({alive _x} count _drugdealers) == 0 && {({alive _x} count _pickup) == 0} && {({alive _x} count _pickupmg) == 0}) exitWith {
			GVAR(sm_winner) = 2;
			GVAR(sm_resolved) = true;
		};
	};
};

if (GVAR(IS_HC_CLIENT)) then {
	[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
};